<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/16
 * Time: 13:59
 */

namespace app\api\model;

use think\Controller;
use think\Model;
use think\Db;
use app\api\model\Hotel;
use app\api\model\TourTicket as tt;
use app\common\model\AppraiseModel as app;

class Tour extends Model
{
    /*
     * 查询景点 玩啥 4个
     */
    public function play($page)
    {
        $res = Db::name('tours')
            ->field('tour_id,tour_name,tour_covers,tour_tag,sale_sum,latitude,longitude')
            ->where('is_del', 0)
            ->where('is_display', 0)
            ->where('is_pass', 1)
            ->where('is_tuijian', 1)
            ->order('sort', 'desc')
            ->limit(($page - 1) * config('page_size'), config('page_size'))
            ->select();
        foreach ($res as $k => $v) {
            $res[$k]['tour_covers'] = addWaterByField($v['tour_covers']);
            if (empty($v['tour_tag'])) {
                $res[$k]['tour_tag'] = [];
            } else {
                $res[$k]['tour_tag'] = explode(',', $v['tour_tag']);
            }
            //查询门票最低价
            $res[$k]['price'] = Db::name('tour_ticket')
                ->field('price')
                ->where('tour_id', $v['tour_id'])
                ->where('is_use', 1)
                ->where('is_del', 0)
                ->min('price');
            //处理景点没上架门票的情况
            if ($res[$k]['price'] == 0) {
                $res[$k]['price'] = '暂无';
            }
            //处理好评率
            $res[$k]['appraise'] = model('AppraiseModel')->getAppRate(2, $v['tour_id']);
            //查看经纬度是否为空
            if (!empty($longitude) && !empty($latitude)) {
                $res[$k]['range'] = getDistance($latitude, $longitude, $v['latitude'], $v['longitude']);
            }

        }
        return $res;
    }

    /*
     * 景点列表
     */
    public function tour_list($where, $page, $status, $longitude = '', $latitude = '', $limit = '')
    {

        $limit = ($limit === '') ? config('page_size') : $limit;
        $res = Db::name('tours')
            ->field('tour_id,tour_name,tour_covers,tour_tag,sale_sum,latitude,longitude')
            ->where('is_pass', 1)
            ->where('is_del', 0)
            ->where('is_display', 0)
            ->where($where)
            ->limit(($page - 1) * $limit, $limit)
            ->select();
        //php获取本月起始时间戳和结束时间戳
//        $thismonth_start=mktime(0,0,0,date('m'),1,date('Y'));
//        $thismonth_end=mktime(23,59,59,date('m'),date('t'),date('Y'));
//        $wheres['create_time'] = ['between time', [$thismonth_start,$thismonth_end]];
        //查询订单
        $tt = new tt();
        foreach ($res as $k => $v) {
            $res[$k]['tour_covers'] = addWaterByField($v['tour_covers']);
            if (empty($v['tour_tag'])) {
                $res[$k]['tour_tag'] = [];
            } else {
                $res[$k]['tour_tag'] = explode(',', $v['tour_tag']);
            }
            //查询门票最低价 
            $ticketArr = Db::name('tour_ticket')
                ->where('tour_id', $v['tour_id'])
                ->where('is_use', 1)
                ->where('is_del', 0)
                ->order("price asc")
                ->limit(0, 1)
                ->find();
            if ($ticketArr['is_discount'] == 1) {
                $res[$k]['price'] = $ticketArr['discount_price'];
            }

            if ($ticketArr['is_discount'] == 0) {
                $res[$k]['price'] = $ticketArr['price'];
            }
            //处理景点没上架门票的情况
            if ($res[$k]['price'] == 0) {
                $res[$k]['price'] = '暂无';
            }
            //处理好评率 
            $res[$k]['appraise'] = model('AppraiseModel')->getAppRate(1, $v['tour_id']);
            //查看经纬度是否为空
            if (!empty($longitude) && !empty($latitude)) {
                $res[$k]['range'] = getDistance($latitude, $longitude, $v['latitude'], $v['longitude']);
            }
        }
        if ($status == 1) {
            //根据某个值对二维数组排序
            $last_names = array_column($res, 'sale_sum');
            array_multisort($last_names, SORT_DESC, $res);
        } else if ($status == 2) {
            $last_names = array_column($res, 'price');
            array_multisort($last_names, SORT_ASC, $res);
        } else if ($status == 3) {
            $last_names = array_column($res, 'appraise');
            array_multisort($last_names, SORT_DESC, $res);
        } else if ($status == 4) {
            $last_names = array_column($res, 'range');
            array_multisort($last_names, SORT_ASC, $res);
        }


        return $res;
    }

    /**
     * [getTourInfo 获取景点的详细信息]
     * @Author   kendy
     * @DateTime 2019-10-30T10:03:18+0800
     * @param    [type]                   $tour_id [景点id]
     * @return   [type]                            [description]
     */
    public function getTourInfo($tour_id)
    {
        $resultData = Db::name('tours')
            ->field('tour_id,tour_name,city,tour_subject,tour_tag,type,sale_sum,should_konwn,tour_covers,tour_time,address,tour_tel,longitude,latitude')
            ->where('tour_id', $tour_id)
            ->find();
        if (!empty($resultData)) {
            $resultData['address'] = $resultData['city'].$resultData['address'];
            unset($resultData['city']);
            $hot = new Hotel();
            $app = new app();
            //获取景点相册 
            $tourImages = Db::name('tour_img')
                ->field('id,img_path')
                ->where('tour_id', $resultData['tour_id'])
                ->order('sort desc')
                ->select();
            if (count($tourImages) > 0) {
                foreach ($tourImages as $key => $value) {
                    $imgArr[] = addWaterByField($value['img_path']);
                }
            }
            $resultData['tour_images'] = $imgArr;
            //获取该产品的好评率
            $resultData['evaluat_rate'] = $app->getAppRate(2, $resultData['tour_id']);
            //景点标签
            $resultData['tour_tag'] = explode(',', $resultData['tour_tag']);
            //获取景点门票
            $ticket['is_del'] = 0;
            $ticket['is_use'] = 1;
            $ticket['tour_id'] = $resultData['tour_id'];
            $ticket['end_time'] = array('egt',date('Y-m-d',time())); //大于
            $ticket['start_time'] = array('elt',date('Y-m-d',time())); //小于
            $resultData['tickets'] = Db::name('tour_ticket')
                ->field('tid,price,tname,category_id,discount_price,is_discount,discount_name_tag,can_refund,start_time,end_time')
                ->where($ticket)
                ->select();
            //获取附近民俗显示距离最近的三个
            $where = 1;
            $resultData['nearby_hotel'] = $hot->hotel_list($where, 1, 4, $resultData['longitude'], $resultData['latitude'], 3);
            //获取该景点的最近一条好评
            $resultData['appraise'] = $app->getAppList(1, $resultData['tour_id'], 1, 1, 1);
            //评价个数
            $resultData['count'] = model('AppraiseModel')->getAppCountByItemId(1, (int)$resultData['tour_id']);
            //相关攻略
            $method['view_id'] = ['like', $resultData['tour_id'] . '%'];
            $resultData['related_method'] = model('method')->method_list($method, 1, 4, 1);
            if (count($resultData['related_method']) > 0) {
                foreach ($resultData['related_method'] as $key => $value) {
                    if (count($value['image']) > 0) {
                        $resultData['related_method'][$key]['image'] = $value['image'][0];
                    }
                }
            }
            return $resultData;
        }
        return $resultData;
    }
}